<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\supplier\model;

use think\Model;
use think\Db;

/**
 * 车型MODEL
 * @package app\supplier\model
 */
class MatorModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'e_supplier_mator_apply';
	
	/**
     * 获取车型列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getMatorList($input)
	{
		$query = Db::name('e_mator')->where('mator_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['field_id']) && !empty($input['field_id'])){
			$query = $query->where('field_id',$input['field_id']);
		}
		if(isset($input['country_id']) && !empty($input['country_id'])){
			$query = $query->where('country_id',$input['country_id']);
		}
		if(isset($input['make_id']) && !empty($input['make_id'])){
			$query = $query->where('make_id',$input['make_id']);
		}
		if(isset($input['f_c_id']) && !empty($input['f_c_id'])){
			$query = $query->where('f_c_id',$input['f_c_id']);
		}
		if(isset($input['series_id']) && !empty($input['series_id'])){
			$query = $query->where('series_id',$input['series_id']);
		}
		if(isset($input['product_id']) && !empty($input['product_id'])){
			$where = array();
			$where['sku_status'] = 0;
			$where['product_id'] = $input['product_id'];
			$mator = Db::name('e_product_sku')->where($where)->select();
			$mator_id_arr = array();
			if(!empty($mator)){
				foreach($mator as $_m){
					array_push($mator_id_arr,$_m['mator_id']);
				}
				$mator_id_arr = array_unique($mator_id_arr);
				$query = $this->where('id','not in',$mator_id_arr);
			}
		}
		if(isset($input['mator_ids']) && !empty($input['mator_ids'])){
			$query = $this->where('id','in',$input['mator_ids']);
		}
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('mator_code|mator_name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=> $list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}	
	
	/**
     * 获取车型列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getMatorSkuList($input)
	{
		//$query = Db::table('e_supplier_product_sku')->where('sku_status','in','[0,1]')->group('mator_id');
		$query = Db::table('e_supplier_product_sku')->where('sku_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['supplier_id']) && !empty($input['supplier_id'])){
			$query = $query->where('supplier_id',$input['supplier_id']);
		}
		if(isset($input['series_id']) && !empty($input['series_id'])){
			//$query = $query->where('series_id',$input['series_id']);
			$mator = Db::table('e_mator')->where('series_id',$input['series_id'])->select();
			$mator_ids = array();
			if(!empty($mator)){
				foreach($mator as $m){
					array_push($mator_ids,$m['id']);
				}
				$query = $query->where('mator_id','in',$mator_ids);
			}
		}
		if(isset($input['keyword']) && !empty($input['keyword'])){
			//$query = $query->where('mator_code|mator_name','like','%'.$input['keyword'].'%');
			$mator = Db::table('e_mator')->where('mator_code|mator_name','like','%'.$input['keyword'].'%')->where('id','in',$mator_ids)->select();
			$mator_ids = array();
			if(!empty($mator)){
				foreach($mator as $m){
					array_push($mator_ids,$m['id']);
				}
				$query = $query->where('mator_id','in',$mator_ids);
			}else{
				$query = $query->where('mator_id','=',0);
			}
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=> $list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$result = $list->toArray();
			if(!empty($result['data'])){
				$mator_ids = array();
				foreach($result['data'] as $v){
					array_push($mator_ids,$v['mator_id']);
				}
				$mator = Db::table('e_mator')->where('id','in',$mator_ids)->select();
				$mator_arr = array();
				foreach($mator as $m){
					$mator_arr[$m['id']] = $m;
				}
				foreach($result['data'] as $k=>$v){
					$mator_id = $v['mator_id'];
					unset($result['data'][$k]);
					$result['data'][$k] = $mator_arr[$mator_id];
				}
			}
			return [
				'list'=>$result,
				'page'=>$list->render()
			];
		}
	}
	
	/**
     * 获取车型列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getSupplierMatorList($input)
	{
		$query = Db::name($this->table)->where('mator_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['supplier_id']) && !empty($input['supplier_id'])){
			$query = $this->where('supplier_id',$input['supplier_id']);
		}
		if(isset($input['field_id']) && !empty($input['field_id'])){
			$query = $this->where('field_id',$input['field_id']);
		}
		if(isset($input['country_id']) && !empty($input['country_id'])){
			$query = $this->where('country_id',$input['country_id']);
		}
		if(isset($input['make_id']) && !empty($input['make_id'])){
			$query = $this->where('make_id',$input['make_id']);
		}
		if(isset($input['f_c_id']) && !empty($input['f_c_id'])){
			$query = $this->where('f_c_id',$input['f_c_id']);
		}
		if(isset($input['series_id']) && !empty($input['series_id'])){
			$query = $this->where('series_id',$input['series_id']);
		}
		if(isset($input['product_id']) && !empty($input['product_id'])){
			$where = array();
			$where['sku_status'] = 0;
			$where['product_id'] = $input['product_id'];
			$mator = Db::name('e_product_sku')->where($where)->select();
			$mator_id_arr = array();
			if(!empty($mator)){
				foreach($mator as $_m){
					array_push($mator_id_arr,$_m['mator_id']);
				}
				$mator_id_arr = array_unique($mator_id_arr);
				$query = $this->where('id','not in',$mator_id_arr);
			}
		}
		if(isset($input['mator_ids']) && !empty($input['mator_ids'])){
			$query = $this->where('id','in',$input['mator_ids']);
		}
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('mator_code|mator_name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=> $list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	/**
     * 获取车型列表
     * @param input $array 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getMators($input)
	{
		return Db::name('e_mator')->where('mator_status','in','[0,1]')->where('id','in',$input['mator_ids'])->select();
	}
	
	/**
     * 获取车型关联产品
     * @param int $mator_id 车型id
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getProductByMator($where)
	{
		return Db::name('e_product_sku')->where($where)->select();
	}
	
	/**
     * 获取车型关联产品
     * @param int $mator_id 车型id
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getSupplierProductSku($where)
	{
		return Db::name('e_supplier_product_sku')->where($where)->select();
	}
}
